<?php
class Server {
  public static function appointment($myPdo, $enquiry, $msgs) {
    $appt_date = '2037-01-01 00:00:00';
    $status = 0;
    $is_all_visited = 1;
    foreach ($msgs as $msg) {
      if ($msg->appointment_date != '0000-00-00 00:00:00') {
        $status = 1;
        if ($msg->is_visited != 1) {
          $is_all_visited = 0;
          if (strtotime($msg->appointment_date) < strtotime($appt_date)) {
            $appt_date = $msg->appointment_date;
          }
        }
      }
    }
    
    $enquiry->appointment_status = $status;
    $enquiry->appointment_date = $appt_date;
    
    if ($enquiry->appointment_status == 1 && $is_all_visited == 1) {
      $enquiry->appointment_status = 2;
    }
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_enquiry', $enquiry);
  }
  
  public static function last_remind($myPdo, $enquiry, $msgs) {
    $enquiry->date_last_remind = (count($msgs) > 0 && $msgs[0]->reminding_date != '0000-00-00 00:00:00') ? $msgs[0]->reminding_date : '0000-00-00 00:00:00';
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_enquiry', $enquiry);
  }
  
  public static function last_visit($myPdo, $enquiry, $msgs) {
  	$visit_msgs = array();
  	foreach ($msgs as &$tmp) {
  	  if ($tmp->is_visited == 1) {
  	    $visit_msgs[] = $tmp;
  	  }
  	}
    $enquiry->date_last_visit = (count($visit_msgs) > 0 && $visit_msgs[0]->appointment_date != '0000-00-00 00:00:00') ? $visit_msgs[0]->appointment_date : '0000-00-00 00:00:00';
    
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_enquiry', $enquiry);
  }
  
  public static function changeMsg($myPdo, $mode, $event, $msgs, $enqs, $enq_msgs) {
    $msg = Server::find_msg($event, $msgs);
    if (isset($msg)) {
	    if ($event->type == 1) {
		    if ($mode == 1) {
		      $msg->appointment_date = '0000-00-00 00:00:00';
		      $msg->appt_user_id = 0;
		      $msg->appt_user_name = '';
		      $msg->appointment_note = '';
		      $msg->is_visited = 0;
		    } else {
		      $msg->appointment_date = $event->start;
		      $msg->appointment_note = $event->description;
		    }
		    DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $msg);
		    
		    $enquiry = Server::find_enquiry($msg, $enqs);
		    $enquiry_msgs = Server::find_enquiry_msgs($enquiry, $enq_msgs);
		    if (isset($enquiry)) {
		      Server::appointment($myPdo, $enquiry, $enquiry_msgs);
		      Server::last_visit($myPdo, $enquiry, $enquiry_msgs);
		    }
	    } else if ($event->type == 2) {
	    	if ($mode == 1) {
	        $msg->reminding_date = '0000-00-00 00:00:00';
	        $msg->reminding_type = 1;
	        $msg->reminding_days = 0;
	        $msg->reminding_note = '';
	        $msg->is_reminding_closed = 0;
	        $msg->reminding_closed_date = '0000-00-00 00:00:00';
	      } else {
	        $msg->reminding_date = $event->start;
	        if ($msg->reminding_type == 2) {
	          $msg->reminding_days = (strtotime(date('Y-m-d', strtotime($msg->reminding_date))) - strtotime(date('Y-m-d', time()))) > 0 ? ((strtotime(date('Y-m-d', strtotime($msg->reminding_date))) - strtotime(date('Y-m-d', time()))) / 86400) + 1 : 0;
	        }
	        $msg->reminding_note = $event->description;
	      }
	      DbUtils::update($myPdo, TABLE_NAME_PREFIX_MODULE . 'data_email_messages', $msg);
	      
	      $enquiry = Server::find_enquiry($msg, $enqs);
        $enquiry_msgs = Server::find_enquiry_msgs($enquiry, $enq_msgs);
        if (isset($enquiry)) {
          Server::last_remind($myPdo, $enquiry, $enquiry_msgs);
        }
	    }
    }
  }
  
  public static function find_msg($evt, $msgs) {
    foreach ($msgs as &$msg) {
      if ($evt->message_id == $msg->id) {
        return $msg;
      }
    }
    return NULL;
  }
  
  public static function find_enquiry($msg, $enqs) {
    foreach ($enqs as &$enq) {
      if ($msg->attr2 == $enq->id) {
        return $enq;
      }
    }
    return NULL;
  }
  
  public static function find_enquiry_msgs($enquiry, $enq_msgs) {
  	$arrs = array();
    foreach ($enq_msgs as &$msg) {
      if ($enquiry->id = $msg->attr2) {
        $arrs[] = $msg;
      }
    }
    return $arrs;
  }
  
}
?>